// ++++++++++++++++++++++++++++++++++++++++
// 《零基础Go语言算法实战》源码
// ++++++++++++++++++++++++++++++++++++++++
// Author:廖显东（ShirDon）
// Blog:https://www.shirdon.com/
// Gitee:https://gitee.com/shirdonl/goAlgorithms.git
// Buy link :https://item.jd.com/14101229.html
// ++++++++++++++++++++++++++++++++++++++++

package main

import (
	"fmt"
	"sort"
)

// 重建队列
func reorderQueue(people [][]int) [][]int {
	sort.Slice(people, func(a, b int) bool {
		if people[a][0] == people[b][0] {
			return people[a][1] < people[b][1]
		}
		return people[a][0] < people[b][0]
	})
	ans := make([][]int, len(people))
	for i := range ans {
		ans[i] = []int{-1, -1}
	}
	for i := 0; i < len(people); i++ {
		index := people[i][1]
		for j := 0; j < len(people); j++ {
			if ans[j][0] == -1 && index == 0 {
				ans[j][0], ans[j][1] = people[i][0], people[i][1]
				break
			} else if ans[j][0] == -1 || ans[j][0] == people[i][0] {
				index--
			}
		}
	}
	return ans
}

func main() {
	people := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
	ret := reorderQueue(people)
	fmt.Println(ret)
}

//$ go run interview3-4.go
//[[-1 -1] [-1 -1] [1 2]]
